<template>
  <el-dialog
    :visible="isShow"
    title="新增员工"
    width="600px"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item prop="username" label="姓名">
        <el-input
          v-model="form.username"
          placeholder="请选择"
        /></el-form-item>
      <el-form-item prop="mobile" label="手机">
        <el-input v-model="form.mobile" placeholder="请选择" />
      </el-form-item>
      <el-form-item prop="timeOfEntry" label="入职时间">
        <el-date-picker v-model="form.timeOfEntry" placeholder="请选择" />
      </el-form-item>
      <el-form-item prop="formOfEmployment" label="聘用形式">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option
            v-for="item in employees.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="workNumber" label="工号">
        <el-input v-model="form.workNumber" placeholder="请选择" />
      </el-form-item>
      <el-form-item prop="departmentName" label="部门">
        <div ref="treeArea" v-clickOutSide="fn">
          <el-input
            v-model="form.departmentName"
            readonly
            placeholder="请选择"
            @focus="focusEvent"
          />
          <el-tree
            v-if="showTree"
            :data="treeData"
            :props="{ label: 'name' }"
            @node-click="nodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item
        prop="correctionTime"
        label="转正时间"
      ><el-date-picker
        v-model="form.correctionTime"
        placeholder="请选择"
      /></el-form-item>
    </el-form>
    <template #footer>
      <div>
        <el-button type="primary" @click="submit">确认</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { companyDepartment } from '@/api/departments.js'
import { sysUserData } from '@/api/employees.js'
import employees from '@/assets/constant/employees.js'
export default {
  name: '',
  props: {
    isShow: { type: Boolean, default: false }
  },
  data() {
    return {
      treeData: [],
      showTree: false,
      // 枚举数据
      employees: employees,
      initForm: '',
      form: {
        //	string	非必须		姓名
        username: '',
        //	string	非必须		手机号
        mobile: '',
        //	string	非必须		入职时间
        timeOfEntry: '',
        //	number	非必须		聘用形式
        formOfEmployment: '',
        //	string	非必须		工号
        workNumber: '',
        //	string	非必须		组织名称
        departmentName: '',
        //	string	非必须		转正时间
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        mobile: [{ required: true, message: '必填', trigger: 'change' }],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        workNumber: [{ required: true, message: '必填', trigger: 'change' }],
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }],
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  mounted() {
    // 监听点击事件
    // document.addEventListener('click', this.clickEvent)
  },
  created() {
    this.initForm = JSON.parse(JSON.stringify(this.form))
  },
  methods: {
    // 提交时
    submit() {
      this.$refs.form.validate(async(res) => {
        if (res) {
          this.$message.success('验证成功')
          await sysUserData(this.form)
          this.$emit('getData')
          this.closeEvent()
        }
      })
    },
    // 部门输入框聚焦时请求数据
    async focusEvent() {
      if (this.treeData.length === 0) {
        const res = await companyDepartment()
        console.log('部门', res)
        this.treeData = this.changeData(res.data.depts)
      }
      this.showTree = true
    },
    // 关闭弹框
    closeEvent() {
      this.$emit('update:isShow', false)
      this.$refs.form.resetFields()
      this.form = JSON.parse(JSON.stringify(this.initForm))
    },
    // 初始数据转化为树形结构
    changeData(arr, str = '') {
      return arr.filter((item) => {
        if (item.pid === str) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // 树形组件点击事件
    nodeClick(data) {
      this.form.departmentName = data.name
    },
    // 点击区域在树形组件外发生时关闭组件
    // clickEvent(e) {
    //   if (!this.$refs.treeArea.contains(e.target)) {
    //     this.showTree = false
    //   }
    // }
    fn() {
      this.showTree = false
    }
  }
}
</script>
<style scoped lang="scss"></style>
